import { _decorator, Component, instantiate, log, Node, Prefab, Sprite, SpriteFrame, v3, Vec2, Vec3 } from 'cc';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { CarriageSF, EPOCH, weapon, WEAPON, WeaponBattleValue } from '../AshFramework/Datas/GameConstant';
import { GameManager } from './GameManager';
import { BundleManager } from '../AshFramework/Managers/BundleManager';
import { BattleManager } from './BattleManager';
import { Weapon } from './Weapon';
const { ccclass, property } = _decorator;

const weaponPos: Vec3[] = [v3(150, -25), v3(135, -65), v3(120, -100), v3(150, 25), v3(135, -15), v3(120, -50), v3(150, 75), v3(135, 35), v3(120, 0)];

@ccclass('Carriage')
export class Carriage extends Component {

    private _epoch: EPOCH = EPOCH.EPOCH_1;
    private _type;
    private _arrWeapons: Array<Node> = new Array();
    private _sprite: Sprite = null;
    private _battleValue: number;


    protected onLoad(): void {
        this._sprite = this.getComponent(Sprite);
        this._epoch = GameManager.Instance.userData.epoch;
    }

    initCarriage(type) {
        this._type = type;
        let name = CarriageSF[this._epoch][type - 1];
        this._battleValue = WeaponBattleValue[GameManager.Instance.userData.epoch][type - 1];
        this._sprite.spriteFrame = BundleManager.AssetMap.get(name) as SpriteFrame;
    }

    creatorWeapon() {
        if (this._arrWeapons.length == 9) return;
        let path = weapon[this._epoch][this._type - 1];
        ResourceUtil.LoadPrefab(path).then((prefab: Prefab) => {
            let weapon: Node = instantiate(prefab);
            this.node.addChild(weapon);
            weapon.getComponent(Weapon).pos = weaponPos[this._arrWeapons.length];
            // weapon.setPosition(weaponPos[this._arrWeapons.length]);
            this._arrWeapons.push(weapon);
            BattleManager.Instance.addBattleValue(this._battleValue);
        })
    }

    removeWeapon() {
        if (this._arrWeapons.length == 0) return;
        BattleManager.Instance.addBattleValue(-this._battleValue);
        this._arrWeapons.pop().destroy();
    }


}


